package base.util.lib;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JSONToString {
	public static String stringToJson(String s) {
		if (s == null) {
			return nullToJson();
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			switch (ch) {
			case '"':
				sb.append("\\\"");
				break;
			case '\\':
				sb.append("\\\\");
				break;
			case '\b':
				sb.append("\\b");
				break;
			case '\f':
				sb.append("\\f");
				break;
			case '\n':
				sb.append("\\n");
				break;
			case '\r':
				sb.append("\\r");
				break;
			case '\t':
				sb.append("\\t");
				break;
			default:
				if (ch >= '\u0000' && ch <= '\u001F') {
					String ss = Integer.toHexString(ch);
					sb.append("\\u");
					for (int k = 0; k < 4 - ss.length(); k++) {
						sb.append('0');
					}
					sb.append(ss.toUpperCase());
				} else {
					sb.append(ch);
				}
			}
		}
		return sb.toString();
	}

	public static String nullToJson() {
		return "";
	}

	public static String objectToJson(Object obj) {
		StringBuffer json = new StringBuffer();
		if (obj == null) {
			json.append("\"\"");
		} else if (obj instanceof Number) {
			json.append(numberToJson((Number) obj));
		} else if (obj instanceof Boolean) {
			json.append(booleanToJson((Boolean) obj));
		} else if (obj instanceof String) {
			json.append("\"").append(stringToJson(obj.toString())).append("\"");
		} else if (obj instanceof Object[]) {
			json.append(arrayToJson((Object[]) obj));
		} else if (obj instanceof List) {
			json.append(listToJson((List) obj));
		} else if (obj instanceof Map) {
			json.append(mapToJson((Map) obj));
		} else if (obj instanceof Set) {
			json.append(setToJson((Set) obj));
		} else {
			json.append(beanToJson(obj));
		}
		return json.toString();
	}

	public static String numberToJson(Number number) {
		return number.toString();
	}

	public static String booleanToJson(Boolean bool) {
		return bool.toString();
	}

	public static String beanToJson(Object bean) {
		StringBuffer json = new StringBuffer();
		json.append("{");
	

			PropertyDescriptor[] props = null;
			try {
				props = Introspector.getBeanInfo(bean.getClass(), Object.class)
						.getPropertyDescriptors();
			} catch (IntrospectionException e) {
			}
			if (props != null) {
				for (int i = 0; i < props.length; i++) {
					try {
						String name = objectToJson(props[i].getName());
						String value = objectToJson(props[i].getReadMethod()
								.invoke(bean, null));
						json.append(name);
						json.append(":");
						json.append(value);
						json.append(",");
					} catch (Exception e) {
					}
				}
				json.setCharAt(json.length() - 1, '}');
			} else {
				json.append("}");
			}
		
		return json.toString();
	}

	public static String listToJson(List list) {
		StringBuffer json = new StringBuffer();
		json.append("[");
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Object obj = list.get(i);
				json.append(objectToJson(obj));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, ']');
		} else {
			json.append("]");
		}
		return json.toString();
	}

	public static String arrayToJson(Object[] array) {
		StringBuffer json = new StringBuffer();
		json.append("[");
		if (array != null && array.length > 0) {
			for (int i = 0; i < array.length; i++) {
				Object obj = array[i];
				json.append(objectToJson(obj));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, ']');
		} else {
			json.append("]");
		}
		return json.toString();
	}

	public static String mapToJson(Map map) {
		StringBuffer json = new StringBuffer();
		json.append("{");
		if (map != null && map.size() > 0) {
			Iterator itr = map.keySet().iterator();
			while (itr.hasNext()) {
				Object entObj = (Object) itr.next();
				json.append(objectToJson(entObj));
				json.append(":");
				json.append(objectToJson(map.get(entObj)));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, '}');
		} else {
			json.append("}");
		}
		return json.toString();
	}

	public static String setToJson(Set set) {
		StringBuffer json = new StringBuffer();
		json.append("[");
		if (set != null && set.size() > 0) {
			Iterator itr = set.iterator();
			while (itr.hasNext()) {
				Object obj = itr.next();
				json.append(objectToJson(obj));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, ']');
		} else {
			json.append("]");
		}
		return json.toString();
	}
}
